package com.Bill;


import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.seeyon.ctp.common.log.CtpLogFactory;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.logging.Log;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.conf.OAConfig;
import com.db.DBConnection;
import com.seeyon.client.CTPRestClient;
import com.seeyon.client.CTPServiceClientManager;

public class BillMainDalzs {
	private static Log log = CtpLogFactory.getLog(BillMainDalzs.class);
	private String ip="http://10.9.112.170";

	public void tbUserBill(String userCode) {
		log.info("tbUserBill进来啦！----------------------------------------------");
		Connection conn = (new DBConnection()).getDefConn();
		Statement stmt = null;

		try {
			stmt = conn.createStatement();
			String url = "http://10.9.6.14:81/billfold/inner/v1/invoice/list";
			SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
			log.info("同步进来时间{}" + sf.format(new Date()));
			JSONObject param = new JSONObject();
			param.put("version", "1.0");
			param.put("nonce", (new Date()).getTime());
			param.put("timestamp", sf.format(new Date()));
			JSONObject data = new JSONObject();
			data.put("userId", userCode);
			data.put("reimbursementStatus", "0");
			data.put("size", "100");
			param.put("data", data.toJSONString().replaceAll("\"", "\\\""));
			log.info("param参数{}:" + param);
			HttpClient httpclient = new HttpClient();
			PostMethod method = new PostMethod(url);
			method.getParams().setParameter("http.protocol.content-charset", "utf-8");
			method.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
			RequestEntity se = new StringRequestEntity(param.toJSONString(), "application/json", "UTF-8");
			method.setRequestEntity(se);
			httpclient.executeMethod(method);
			JSONObject obj = JSONObject.parseObject(method.getResponseBodyAsString());
			log.info("rest_ip地址=" + OAConfig.rest_ip);
			CTPServiceClientManager clientManager = CTPServiceClientManager.getInstance(OAConfig.rest_ip);
			CTPRestClient client = clientManager.getRestClient();
			client.authenticate(OAConfig.rest_user, OAConfig.rest_pw);
			log.info("user用户=" + OAConfig.rest_user + "，密码=" + OAConfig.rest_pw);
			HashMap<String, String> map = new HashMap();
			map.put("template1_jdcxstyfp", "8828910654929716621");
			map.put("template3_tydzfp", "-8393781134982626213");
			map.put("template3_escxstyfp", "-147723167457721019");
			map.put("template13_qdzp_pufp", "-8516037866594220649");
			map.put("template5 _hkkyjpxcd", "-9018010787355407638");
			map.put("ylmzsf", "9130765416932855210");
			map.put("ghjfsrpj", "961999285307785631");
			map.put("template7_czcdcfp", "-8619797417291133098");
			map.put("template1_zzszyfp", "-3590849780715666590");
			map.put("template1_zzsdzzyfp", "-7607217821669899045");
			map.put("template2_zzsdzpufptxf", "8691271132346404696");
			map.put("template2_zzsptfpdz", "-2177024964106040201");
			map.put("template8_txf", "1798627781992136878");
			map.put("template13_qdzp_zyfp", "1672597103010991009");
			map.put("template2_zzsptfp", "4377169692631843374");
			map.put("template6_kyfp", "-7434065684522185445");
			map.put("template2_zzsptfpjs", "7291312610223344288");
			map.put("template9_defp", "-4933097012863940367");
			map.put("template12_qdp_pufp", "-3352159344171072688");
			map.put("template4_hcp", "2674856612350152331");
			map.put("fssrtypj", "-8788971208442099252");
			map.put("shbxjpj", "4338872093768761430");
			map.put("others", "3239038616031543793");
			map.put("template3_tyjdfp", "113359747307028531");
			map.put("template12_qdp_zyfp", "4253780008688173548");
			map.put("fssrzypj", "5639946572428758242");
			map.put("gysyjz", "4928777874412487920");
			map.put("ylzysf", "-6072550200266438148");
			map.put("zjwl", "-2387447848506143853");
			map.put("shtthf", "5651041469633743339");
			log.info("返回的数据{}:" + obj);
			if (obj.getString("code").equals("200")) {
				JSONArray list = obj.getJSONObject("data").getJSONArray("list");
				log.info("i循环开始啦！----------------------------------------------");
				for(int i = 0; i < list.size(); ++i) {
					JSONObject bill = list.getJSONObject(i);
					log.info("第" + i + "次循环--------------------");
					log.info("发票信息{}:" + bill);
					Document doc = DocumentHelper.createDocument();
					Element forms = doc.addElement("forms");
					forms.addAttribute("version", "2.1");
					Element root = forms.addElement("formExport");
					Element element = root.addElement("summary");
					element.addAttribute("id", "1309748970347515697");
					element.addAttribute("name", "formmain_0466");
					element = root.addElement("definitions");
					element.addAttribute("valuetype", "10");
					element.addAttribute("isnull", "true");
					element.addAttribute("length", "0");
					element = root.addElement("values");

					try {
						Element column = element.addElement("column");
						column.addAttribute("name", "发票id");
						Element value = column.addElement("value");
						value.setText(this.nullToObject(bill.getString("id")));
						column = element.addElement("column");
						column.addAttribute("name", "发票代码");
						value = column.addElement("value");
						value.setText(this.nullToObject(bill.getString("invoiceCode")));
						column = element.addElement("column");
						column.addAttribute("name", "发票号");
						value = column.addElement("value");
						value.setText(this.nullToObject(bill.getString("invoiceNumber")));
						column = element.addElement("column");
						column.addAttribute("name", "发票类型");
						value = column.addElement("value");
						value.setText(this.nullToObject(map.get(bill.getString("invoiceCategory"))));
						column = element.addElement("column");
						column.addAttribute("name", "发票或票据");
						value = column.addElement("value");
						value.setText(this.nullToObject(bill.getString("invoiceOrBillOrder")));
						column = element.addElement("column");
						column.addAttribute("name", "开票时间");
						value = column.addElement("value");
						value.setText(this.nullToObject(bill.getString("invoiceTime")));
						column = element.addElement("column");
						column.addAttribute("name", "采集时间");
						value = column.addElement("value");
						value.setText(this.nullToObject(bill.getString("collectionTime")));
						column = element.addElement("column");
						column.addAttribute("name", "发票状态");
						value = column.addElement("value");
						value.setText(this.nullToObject(bill.getString("invoiceStatus")));
						column = element.addElement("column");
						column.addAttribute("name", "是否已查验");
						value = column.addElement("value");
						value.setText(this.nullToObject(bill.getString("checkFlag")));
						column = element.addElement("column");
						column.addAttribute("name", "报销状态");
						value = column.addElement("value");
						value.setText("4828030541132023061");
						column = element.addElement("column");
						column.addAttribute("name", "发票金额");
						value = column.addElement("value");
						value.setText(this.nullToObject(bill.getString("taxAmount")));
						column = element.addElement("column");
						column.addAttribute("name", "不含税金额");
						value = column.addElement("value");
						value.setText(this.nullToObject(bill.getString("taxFreeAmount")));
						column = element.addElement("column");
						column.addAttribute("name", "合计税额");
						value = column.addElement("value");
						value.setText(this.nullToObject(bill.getString("totalTax")));
						column = element.addElement("column");
						column.addAttribute("name", "机动车发票税率");
						value = column.addElement("value");
						value.setText(this.nullToObject(bill.getString("motorRate")));
						column = element.addElement("column");
						column.addAttribute("name", "备注");
						value = column.addElement("value");
						value.setText(this.nullToObject(bill.getString("memo")));
						column = element.addElement("column");
						column.addAttribute("name", "销方名称");
						value = column.addElement("value");
						value.setText(this.nullToObject(bill.getString("salerName")));
						column = element.addElement("column");
						column.addAttribute("name", "销方纳税人识别号");
						value = column.addElement("value");
						value.setText(this.nullToObject(bill.getString("salerTaxNo")));
						column = element.addElement("column");
						column.addAttribute("name", "销售方电话");
						value = column.addElement("value");
						value.setText(this.nullToObject(bill.getString("salerTelephone")));
						column = element.addElement("column");
						column.addAttribute("name", "销售方地址");
						value = column.addElement("value");
						value.setText(this.nullToObject(bill.getString("salerAddress")));
						column = element.addElement("column");
						column.addAttribute("name", "销售方开户行");
						value = column.addElement("value");
						value.setText(this.nullToObject(bill.getString("salerBankBranch")));
						column = element.addElement("column");
						column.addAttribute("name", "销售方账号");
						value = column.addElement("value");
						value.setText(this.nullToObject(bill.getString("salerBankCardNo")));
						column = element.addElement("column");
						column.addAttribute("name", "购方名称乘客姓名");
						value = column.addElement("value");
						value.setText(this.nullToObject(bill.getString("buyerName")));
						column = element.addElement("column");
						column.addAttribute("name", "购方纳税人识别号");
						value = column.addElement("value");
						value.setText(this.nullToObject(bill.getString("buyerTaxNo")));
						column = element.addElement("column");
						column.addAttribute("name", "购买方电话");
						value = column.addElement("value");
						value.setText(this.nullToObject(bill.getString("buyerTelephone")));
						column = element.addElement("column");
						column.addAttribute("name", "购买方地址");
						value = column.addElement("value");
						value.setText(this.nullToObject(bill.getString("buyerAddress")));
						column = element.addElement("column");
						column.addAttribute("name", "购买方开户行");
						value = column.addElement("value");
						value.setText(this.nullToObject(bill.getString("buyerBankBranch")));
						column = element.addElement("column");
						column.addAttribute("name", "购买方账号");
						value = column.addElement("value");
						value.setText(this.nullToObject(bill.getString("buyerBankCardNo")));
						column = element.addElement("column");
						column.addAttribute("name", "附件文本");
						value = column.addElement("value");
						value.setText(this.nullToObject(bill.getString("invoiceUrl")));
						column = element.addElement("column");
						column.addAttribute("name", "校验码");
						value = column.addElement("value");
						value.setText(this.nullToObject(bill.getString("checkCode")));
						column = element.addElement("column");
						column.addAttribute("name", "发票所在地");
						value = column.addElement("value");
						value.setText(this.nullToObject(bill.getString("invoiceLocation")));
						column = element.addElement("column");
						column.addAttribute("name", "里程");
						value = column.addElement("value");
						value.setText(this.nullToObject(bill.getString("mileage")));
						column = element.addElement("column");
						column.addAttribute("name", "燃油附加费");
						value = column.addElement("value");
						value.setText(this.nullToObject(bill.getString("fuelSurcharge")));
						column = element.addElement("column");
						column.addAttribute("name", "其他税费");
						value = column.addElement("value");
						value.setText(this.nullToObject(bill.getString("otherTaxAmount")));
						column = element.addElement("column");
						column.addAttribute("name", "航班号");
						value = column.addElement("value");
						value.setText(this.nullToObject(bill.getString("flightNo")));
						column = element.addElement("column");
						column.addAttribute("name", "火车票Id");
						value = column.addElement("value");
						value.setText(this.nullToObject(bill.getString("ticketNo")));
						column = element.addElement("column");
						column.addAttribute("name", "火车票红色编码");
						value = column.addElement("value");
						value.setText(this.nullToObject(bill.getString("ticketCode")));
						column = element.addElement("column");
						column.addAttribute("name", "座位类别");
						value = column.addElement("value");
						value.setText(this.nullToObject(bill.getString("seatLevel")));
						column = element.addElement("column");
						column.addAttribute("name", "出发地");
						value = column.addElement("value");
						value.setText(this.nullToObject(bill.getString("departure")));
						column = element.addElement("column");
						column.addAttribute("name", "目的地");
						value = column.addElement("value");
						value.setText(this.nullToObject(bill.getString("destination")));
						column = element.addElement("column");
						column.addAttribute("name", "车次");
						value = column.addElement("value");
						value.setText(this.nullToObject(bill.getString("trainNo")));
						column = element.addElement("column");
						column.addAttribute("name", "座位号");
						value = column.addElement("value");
						value.setText(this.nullToObject(bill.getString("seatNo")));
						column = element.addElement("column");
						column.addAttribute("name", "检票口");
						value = column.addElement("value");
						value.setText(this.nullToObject(bill.getString("checkIn")));
						column = element.addElement("column");
						column.addAttribute("name", "上车时间");
						value = column.addElement("value");
						value.setText(this.nullToObject(bill.getString("boardingTime")));
						column = element.addElement("column");
						column.addAttribute("name", "下车时间");
						value = column.addElement("value");
						value.setText(this.nullToObject(bill.getString("alightingTime")));
						column = element.addElement("column");
						column.addAttribute("name", "车号");
						value = column.addElement("value");
						value.setText(this.nullToObject(bill.getString("carNo")));
						column = element.addElement("column");
						column.addAttribute("name", "填开单位");
						value = column.addElement("value");
						value.setText(this.nullToObject(bill.getString("invoiceCompany")));
						column = element.addElement("column");
						column.addAttribute("name", "填开日期");
						value = column.addElement("value");
						value.setText(this.nullToObject(bill.getString("invoiceDate")));
						column = element.addElement("column");
						column.addAttribute("name", "民航发展基金");
						value = column.addElement("value");
						value.setText(this.nullToObject(bill.getString("fundAmount")));
						column = element.addElement("column");
						column.addAttribute("name", "保险费");
						value = column.addElement("value");
						value.setText(this.nullToObject(bill.getString("premium")));
						column = element.addElement("column");
						column.addAttribute("name", "交款人名称");
						value = column.addElement("value");
						value.setText(this.nullToObject(bill.getString("payerPartyName")));
						column = element.addElement("column");
						column.addAttribute("name", "票面身份证号码");
						value = column.addElement("value");
						value.setText(this.nullToObject(bill.getString("payerPartyCode")));
						column = element.addElement("column");
						column.addAttribute("name", "交款人类型");
						value = column.addElement("value");
						value.setText(this.nullToObject(bill.getString("payerPartyType")));
						column = element.addElement("column");
						column.addAttribute("name", "开票单位全称");
						value = column.addElement("value");
						value.setText(this.nullToObject(bill.getString("invoicingPartyName")));
						column = element.addElement("column");
						column.addAttribute("name", "开票单位代码");
						value = column.addElement("value");
						value.setText(this.nullToObject(bill.getString("invoicingPartyCode")));
						Element subForms = root.addElement("subForms");
						Element subForm = subForms.addElement("subForm");
						subForm.addElement("definitions");
						element = subForm.addElement("values");
						JSONArray detailVoList = bill.getJSONArray("personalInvoiceDetailVoList");

						log.info("j循环开始啦！--------------------------------");
						for(int j = 0; j < detailVoList.size(); ++j) {
							JSONObject detail = detailVoList.getJSONObject(j);
							log.info("第" + j + "次循环");
							log.info("发票子表信息{}:" + detail);
							Element row = element.addElement("row");
							column = row.addElement("column");
							column.addAttribute("name", "单据明细序号");
							value = column.addElement("value");
							value.setText(this.nullToObject(detail.getString("djmxxh")));
							column = row.addElement("column");
							column.addAttribute("name", "发票行性质");
							value = column.addElement("value");
							value.setText(this.nullToObject(detail.getString("fphxz")));
							column = row.addElement("column");
							column.addAttribute("name", "商品名称");
							value = column.addElement("value");
							value.setText(this.nullToObject(detail.getString("goodsName")));
							column = row.addElement("column");
							column.addAttribute("name", "数量");
							value = column.addElement("value");
							value.setText(this.nullToObject(detail.getString("num")));
							column = row.addElement("column");
							column.addAttribute("name", "单价");
							value = column.addElement("value");
							value.setText(this.nullToObject(detail.getString("price")));
							column = row.addElement("column");
							column.addAttribute("name", "税率");
							value = column.addElement("value");
							value.setText(this.nullToObject(detail.getString("taxRate")));
							column = row.addElement("column");
							column.addAttribute("name", "规格型号");
							value = column.addElement("value");
							value.setText(this.nullToObject(detail.getString("spec")));
							column = row.addElement("column");
							column.addAttribute("name", "单位");
							value = column.addElement("value");
							value.setText(this.nullToObject(detail.getString("unit")));
							column = row.addElement("column");
							column.addAttribute("name", "商品编码");
							value = column.addElement("value");
							value.setText(this.nullToObject(detail.getString("spbm")));
							column = row.addElement("column");
							column.addAttribute("name", "优惠政策标识");
							value = column.addElement("value");
							value.setText(this.nullToObject(detail.getString("yhzcbs")));
							column = row.addElement("column");
							column.addAttribute("name", "普通零税率");
							value = column.addElement("value");
							value.setText(this.nullToObject(detail.getString("lslbs")));
							column = row.addElement("column");
							column.addAttribute("name", "增值税特殊管理");
							value = column.addElement("value");
							value.setText(this.nullToObject(detail.getString("zzstsgl")));
							column = row.addElement("column");
							column.addAttribute("name", "自行编码");
							value = column.addElement("value");
							value.setText(this.nullToObject(detail.getString("zxbm")));
							column = row.addElement("column");
							column.addAttribute("name", "扣除额");
							value = column.addElement("value");
							value.setText(this.nullToObject(detail.getString("kce")));
							column = row.addElement("column");
							column.addAttribute("name", "税额");
							value = column.addElement("value");
							value.setText(this.nullToObject(detail.getString("tax")));
							column = row.addElement("column");
							column.addAttribute("name", "不含税金额");
							value = column.addElement("value");
							value.setText(this.nullToObject(detail.getString("taxFreeAmount")));
							column = row.addElement("column");
							column.addAttribute("name", "含税金额");
							value = column.addElement("value");
							value.setText(this.nullToObject(detail.getString("taxAmount")));
							column = row.addElement("column");
							column.addAttribute("name", "不含税单价");
							value = column.addElement("value");
							value.setText(this.nullToObject(detail.getString("taxFreePrice")));
							column = row.addElement("column");
							column.addAttribute("name", "商品编码简称");
							value = column.addElement("value");
							value.setText(this.nullToObject(detail.getString("spbm_abb")));
							column = row.addElement("column");
							column.addAttribute("name", "归集时间");
							value = column.addElement("value");
							value.setText(this.nullToObject(detail.getString("collectionTime")));
						}

						log.info("j循环结束啦！--------------------------------");

						Map para = new HashMap();
						para.put("templateCode", "pjxx01");
						para.put("loginName", "yysjs");
						para.put("dataXml", doc.asXML());
						String var33 = (String)client.post("form/import/pjxx01", para, String.class);
					} catch (Exception var30) {
						var30.printStackTrace();
					}
				}
			}
			log.info("结束时间{}" + sf.format(new Date()));
		} catch (Exception var31) {
			var31.printStackTrace();
		}
		log.info("同步发票方法结束啦！---------------------------------------------------------------");

	}

	public void getUserBill(String userCode) {
		log.info("获取用户发票开始啦！------------------------------------------------------------");
		log.info("传进来的usercode=" + userCode);
		Connection conn = (new DBConnection()).getDefConn();
		Statement stmt = null;

		try {
			stmt = conn.createStatement();
			String url = this.ip + "/billfold/inner/v1/invoice/list";
			log.info("访问地址{}" +  url);
			SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
			log.info("获取用户发票开始时间{}" + sf.format(new Date()));
			HashMap<String, String> map = new HashMap();
			map.put("template1_jdcxstyfp", "8828910654929716621");
			map.put("template3_tydzfp", "-8393781134982626213");
			map.put("template3_escxstyfp", "-147723167457721019");
			map.put("template13_qdzp_pufp", "-8516037866594220649");
			map.put("template5 _hkkyjpxcd", "-9018010787355407638");
			map.put("ylmzsf", "9130765416932855210");
			map.put("ghjfsrpj", "961999285307785631");
			map.put("template7_czcdcfp", "-8619797417291133098");
			map.put("template1_zzszyfp", "-3590849780715666590");
			map.put("template1_zzsdzzyfp", "-7607217821669899045");
			map.put("template2_zzsdzpufptxf", "8691271132346404696");
			map.put("template2_zzsptfpdz", "-2177024964106040201");
			map.put("template8_txf", "1798627781992136878");
			map.put("template13_qdzp_zyfp", "1672597103010991009");
			map.put("template2_zzsptfp", "4377169692631843374");
			map.put("template6_kyfp", "-7434065684522185445");
			map.put("template2_zzsptfpjs", "7291312610223344288");
			map.put("template9_defp", "-4933097012863940367");
			map.put("template12_qdp_pufp", "-3352159344171072688");
			map.put("template4_hcp", "2674856612350152331");
			map.put("fssrtypj", "-8788971208442099252");
			map.put("shbxjpj", "4338872093768761430");
			map.put("others", "3239038616031543793");
			map.put("template3_tyjdfp", "113359747307028531");
			map.put("template12_qdp_zyfp", "4253780008688173548");
			map.put("fssrzypj", "5639946572428758242");
			map.put("gysyjz", "4928777874412487920");
			map.put("ylzysf", "-6072550200266438148");
			map.put("zjwl", "-2387447848506143853");
			map.put("shtthf", "5651041469633743339");
			stmt.executeUpdate("delete from a_sew_bill where usercode='" + userCode + "'");
			int page = 1;
			int pageSize = 100;

			StringBuffer insert;
			for(insert = new StringBuffer(); pageSize == 100; ++page) {
				pageSize = 0;
				JSONObject param = new JSONObject();
				param.put("version", "1.0");
				param.put("nonce", (new Date()).getTime());
				param.put("timestamp", sf.format(new Date()));
				JSONObject data = new JSONObject();
				data.put("userId", userCode);
				data.put("reimbursementStatus", "0");
				data.put("size", "100");
				data.put("page", page);
				param.put("data", data.toJSONString().replaceAll("\"", "\\\""));
				log.info("param参数{}" + param);
				HttpClient httpclient = new HttpClient();
				PostMethod method = new PostMethod(url);
				method.getParams().setParameter("http.protocol.content-charset", "utf-8");
				method.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
				RequestEntity se = new StringRequestEntity(param.toJSONString(), "application/json", "UTF-8");
				method.setRequestEntity(se);
				httpclient.executeMethod(method);
				JSONObject obj = JSONObject.parseObject(method.getResponseBodyAsString());
				if (obj.getString("code").equals("200")) {
					JSONArray list = obj.getJSONObject("data").getJSONArray("list");
					pageSize = list.size();

					for(int i = 0; i < list.size(); ++i) {
						JSONObject bill = list.getJSONObject(i);
						if (!this.nullToObject(bill.getString("isReimbursed")).equals("true")) {
							insert.append("insert into a_sew_bill(usercode,id,invoiceCode,invoiceNumber,invoiceCategory,invoiceOrBillOrder,invoiceTime,collectionTime,invoiceStatus,checkFlag,reimbursementStatus,taxAmount,taxFreeAmount,totalTax,motorRate,memo,salerName,salerTaxNo,salerTelephone,salerAddress,salerBankBranch,salerBankCardNo,buyerName,buyerTaxNo,buyerTelephone,buyerAddress,buyerBankBranch,buyerBankCardNo,invoiceUrl,pdfUrl,checkCode,invoiceLocation,mileage,fuelSurcharge,otherTaxAmount,flightNo,ticketNo,ticketCode,seatLevel,departure,destination,trainNo,seatNo,checkIn,boardingTime,alightingTime,carNo,invoiceCompany,invoiceDate,fundAmount,premium,payerPartyName,payerPartyCode,payerPartyType,invoicingPartyName,invoicingPartyCode,editFlag) values ('" + userCode + "'" + ",'" + this.nullToObject(bill.getString("id")) + "'" + ",'" + this.nullToObject(bill.getString("invoiceCode")) + "'" + ",'" + this.nullToObject(bill.getString("invoiceNumber")) + "'" + ",'" + this.nullToObject(bill.getString("invoiceCategory")) + "'" + ",'" + this.nullToObject(bill.getString("invoiceOrBillOrder")) + "'" + ",'" + this.nullToObject(bill.getString("invoiceTime")) + "'" + ",'" + this.nullToObject(bill.getString("collectionTime")) + "'" + ",'" + this.nullToObject(bill.getString("invoiceStatus")) + "'" + ",'" + this.nullToObject(bill.getString("checkFlag")) + "'" + ",'" + this.nullToObject(bill.getString("reimbursementStatus")) + "'" + ",'" + this.nullToObject(bill.getString("taxAmount")) + "'" + ",'" + this.nullToObject(bill.getString("taxFreeAmount")) + "'" + ",'" + this.nullToObject(bill.getString("totalTax")) + "'" + ",'" + this.nullToObject(bill.getString("motorRate")) + "'" + ",'" + this.nullToObject(bill.getString("memo")) + "'" + ",'" + this.nullToObject(bill.getString("salerName")) + "'" + ",'" + this.nullToObject(bill.getString("salerTaxNo")) + "'" + ",'" + this.nullToObject(bill.getString("salerTelephone")) + "'" + ",'" + this.nullToObject(bill.getString("salerAddress")) + "'" + ",'" + this.nullToObject(bill.getString("salerBankBranch")) + "'" + ",'" + this.nullToObject(bill.getString("salerBankCardNo")) + "'" + ",'" + this.nullToObject(bill.getString("buyerName")) + "'" + ",'" + this.nullToObject(bill.getString("buyerTaxNo")) + "'" + ",'" + this.nullToObject(bill.getString("buyerTelephone")) + "'" + ",'" + this.nullToObject(bill.getString("buyerAddress")) + "'" + ",'" + this.nullToObject(bill.getString("buyerBankBranch")) + "'" + ",'" + this.nullToObject(bill.getString("buyerBankCardNo")) + "'" + ",'" + this.nullToObject(bill.getString("invoiceUrl")) + "'" + ",'" + this.nullToObject(bill.getString("pdfUrl")) + "'" + ",'" + this.nullToObject(bill.getString("checkCode")) + "'" + ",'" + this.nullToObject(bill.getString("invoiceLocation")) + "'" + ",'" + this.nullToObject(bill.getString("mileage")) + "'" + ",'" + this.nullToObject(bill.getString("fuelSurcharge")) + "'" + ",'" + this.nullToObject(bill.getString("otherTaxAmount")) + "'" + ",'" + this.nullToObject(bill.getString("flightNo")) + "'" + ",'" + this.nullToObject(bill.getString("ticketNo")) + "'" + ",'" + this.nullToObject(bill.getString("ticketCode")) + "'" + ",'" + this.nullToObject(bill.getString("seatLevel")) + "'" + ",'" + this.nullToObject(bill.getString("departure")) + "'" + ",'" + this.nullToObject(bill.getString("destination")) + "'" + ",'" + this.nullToObject(bill.getString("trainNo")) + "'" + ",'" + this.nullToObject(bill.getString("seatNo")) + "'" + ",'" + this.nullToObject(bill.getString("checkIn")) + "'" + ",'" + this.nullToObject(bill.getString("boardingTime")) + "'" + ",'" + this.nullToObject(bill.getString("alightingTime")) + "'" + ",'" + this.nullToObject(bill.getString("carNo")) + "'" + ",'" + this.nullToObject(bill.getString("invoiceCompany")) + "'" + ",'" + this.nullToObject(bill.getString("invoiceDate")) + "'" + ",'" + this.nullToObject(bill.getString("fundAmount")) + "'" + ",'" + this.nullToObject(bill.getString("premium")) + "'" + ",'" + this.nullToObject(bill.getString("payerPartyName")) + "'" + ",'" + this.nullToObject(bill.getString("payerPartyCode")) + "'" + ",'" + this.nullToObject(bill.getString("payerPartyType")) + "'" + ",'" + this.nullToObject(bill.getString("invoicingPartyName")) + "'" + ",'" + this.nullToObject(bill.getString("invoicingPartyCode")) + "'" + ",'" + this.nullToObject(bill.getString("editFlag")) + "'" + ");");
						}
					}
				}

				log.info("sql语句{}" + insert);
			}

			if (!insert.toString().equals("")) {
				log.info("执行修改进来啦！-----------------------");
				stmt.executeUpdate(insert.toString());
			}
			log.info("获取用户发票结束时间{}" + sf.format(new Date()));
		} catch (Exception var19) {
			var19.printStackTrace();
		}

		log.info("获取用户发票方法结束啦！---------------------------------------------------------------");
	}

	public void tbBillState() {
		log.info("同步发票状态开始--------------------------------------------------------------");
		Connection conn = (new DBConnection()).getDefConn();
		Statement stmt = null;

		try {
			stmt = conn.createStatement();
			SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
			SimpleDateFormat sf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			log.info("同步发票状态开始时间{}" + sf2.format(new Date()));
			String modifyDate = sf2.format(new Date());

			ResultSet res;
			for(res = stmt.executeQuery("select createdate from hrlog where type='发票状态更新'"); res.next(); modifyDate = res.getString(1)) {
			}
			log.info("res信息{}" + res);
			stmt.executeUpdate("delete from hrlog where type='发票状态更新'");
			stmt.executeUpdate("insert into hrlog(type,createdate) values ('发票状态更新',getdate())");
			res = stmt.executeQuery("select m.id,field0059 员工工号,field0451 税号,u.name 所属单位,field0057 单据编号,s.state 状态,a.state 待办状态,a.sub_state 处理状态  from formmain_0706 m,org_unit u,col_summary s,ctp_affair a  where u.id=field0321  and s.form_recordid=m.id  and a.object_id = s.id and a.app=1  and a.update_date>='" + modifyDate + "' order by a.update_date ");
			log.info("查询formmain_0706表信息{}" + res);
			JSONArray dataList = new JSONArray();

			JSONObject obj;
			while(res.next()) {
				obj = new JSONObject();
				obj.put("userId", res.getString("员工工号"));
				obj.put("agencyCode", res.getString("税号"));
				obj.put("agencyName", res.getString("所属单位"));
				obj.put("reimbursementId", res.getString("单据编号"));
				if (res.getString("待办状态") != null && res.getString("待办状态").equals("1") && (res.getString("处理状态").equals("3") || res.getString("处理状态").equals("16") || res.getString("处理状态").equals("18"))) {
					obj.put("reimbursementStatus", "0");
				} else if (res.getString("状态") != null && res.getString("状态").equals("3")) {
					obj.put("reimbursementStatus", "2");
				} else if (res.getString("状态") != null && res.getString("状态").equals("0")) {
					obj.put("reimbursementStatus", "1");
				} else {
					obj.put("reimbursementStatus", "0");
				}

				obj.put("flowCode", this.nullToObject(res.getString("单据编号")));
				obj.put("id", res.getString("id"));
				dataList.add(obj);
			}
			log.info("dataList信息{}" + dataList);
			JSONArray invoiceList;
			JSONObject param;
			HttpClient httpclient;
			PostMethod method;
			StringRequestEntity se;
			int i;
			String url;
			for(i = 0; i < dataList.size(); ++i) {
				res = stmt.executeQuery("select field0432 as invoiceId,'a' from formson_0709 where formmain_id = " + dataList.getJSONObject(i).getString("id") + " " + " union select field0007 as invoiceId,'b' from formson_0710 where formmain_id = " + dataList.getJSONObject(i).getString("id") + " " + " union select field0433 as invoiceId,'c' from formson_0711 where formmain_id = " + dataList.getJSONObject(i).getString("id"));

				invoiceList = new JSONArray();

				while(res.next()) {
					if (res.getString("invoiceId") != null) {
						obj = new JSONObject();
						obj.put("invoiceId", res.getString(1));
						invoiceList.add(obj);
					}
				}

				dataList.getJSONObject(i).put("invoiceList", invoiceList);
				url = this.ip + "/billfold/inner/v1/invoice/operateReimbursementStatus";
				param = new JSONObject();
				param.put("version", "1.0");
				param.put("nonce", (new Date()).getTime());
				param.put("timestamp", sf.format(new Date()));
				param.put("data", dataList.getJSONObject(i).toJSONString().replaceAll("\"", "\\\""));
				httpclient = new HttpClient();
				method = new PostMethod(url);
				method.getParams().setParameter("http.protocol.content-charset", "utf-8");
				method.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
				se = new StringRequestEntity(param.toJSONString(), "application/json", "UTF-8");
				method.setRequestEntity(se);
				httpclient.executeMethod(method);
				obj = JSONObject.parseObject(method.getResponseBodyAsString());
				log.info("formmain_0706单据编号:" + dataList.getJSONObject(i).getString("flowCode") + ",状态:" + dataList.getJSONObject(i).getString("reimbursementStatus") + ",结果:" + obj);
			}

			res = stmt.executeQuery("select m.id,field0068 员工工号,field0227 税号,u.name 所属单位,field0001 单据编号,s.state 状态,a.state 待办状态,a.sub_state 处理状态  from formmain_0722 m,org_unit u,col_summary s,ctp_affair a  where u.id=field0070  and s.form_recordid=m.id  and a.object_id = s.id and a.app=1  and a.update_date>='" + modifyDate + "' order by a.update_date ");
			dataList = new JSONArray();

			while(res.next()) {
				obj = new JSONObject();
				obj.put("userId", res.getString("员工工号"));
				obj.put("agencyCode", res.getString("税号"));
				obj.put("agencyName", res.getString("所属单位"));
				obj.put("reimbursementId", res.getString("单据编号"));
				if (res.getString("待办状态") == null || !res.getString("待办状态").equals("1") || !res.getString("处理状态").equals("3") && !res.getString("处理状态").equals("16") && !res.getString("处理状态").equals("18")) {
					if (res.getString("状态") != null && res.getString("状态").equals("3")) {
						obj.put("reimbursementStatus", "2");
					} else if (res.getString("状态") != null && res.getString("状态").equals("0")) {
						obj.put("reimbursementStatus", "1");
					} else {
						obj.put("reimbursementStatus", "0");
					}
				} else {
					obj.put("reimbursementStatus", "0");
				}

				obj.put("flowCode", this.nullToObject(res.getString("单据编号")));
				obj.put("id", res.getString("id"));
				dataList.add(obj);
			}
			log.info("查询formmain_0722表信息{}" + dataList);
			for(i = 0; i < dataList.size(); ++i) {
				res = stmt.executeQuery("select field0192 as invoiceId,'0723' from formson_0723 where formmain_id = " + dataList.getJSONObject(i).getString("id"));
				invoiceList = new JSONArray();

				while(res.next()) {
					if (res.getString("invoiceId") != null) {
						obj = new JSONObject();
						obj.put("invoiceId", res.getString(1));
						invoiceList.add(obj);
					}
				}

				dataList.getJSONObject(i).put("invoiceList", invoiceList);
				url = this.ip + "/billfold/inner/v1/invoice/operateReimbursementStatus";
				param = new JSONObject();
				param.put("version", "1.0");
				param.put("nonce", (new Date()).getTime());
				param.put("timestamp", sf.format(new Date()));
				param.put("data", dataList.getJSONObject(i).toJSONString().replaceAll("\"", "\\\""));
				httpclient = new HttpClient();
				method = new PostMethod(url);
				method.getParams().setParameter("http.protocol.content-charset", "utf-8");
				method.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
				se = new StringRequestEntity(param.toJSONString(), "application/json", "UTF-8");
				method.setRequestEntity(se);
				httpclient.executeMethod(method);
				obj = JSONObject.parseObject(method.getResponseBodyAsString());
				log.info("formmain_0722单据编号:" + dataList.getJSONObject(i).getString("flowCode") + ",状态:" + dataList.getJSONObject(i).getString("reimbursementStatus") + ",结果:" + obj);
			}

			res = stmt.executeQuery("select m.id,field0068 员工工号,field0119 税号,u.name 所属单位,field0001 单据编号,s.state 状态,a.state 待办状态,a.sub_state 处理状态  from formmain_0728 m,org_unit u,col_summary s,ctp_affair a  where u.id=field0070  and s.form_recordid=m.id  and a.object_id = s.id and a.app=1  and a.update_date>='" + modifyDate + "' order by a.update_date ");
			dataList = new JSONArray();

			while(res.next()) {
				obj = new JSONObject();
				obj.put("userId", res.getString("员工工号"));
				obj.put("agencyCode", res.getString("税号"));
				obj.put("agencyName", res.getString("所属单位"));
				obj.put("reimbursementId", res.getString("单据编号"));
				if (res.getString("待办状态") != null && res.getString("待办状态").equals("1") && (res.getString("处理状态").equals("3") || res.getString("处理状态").equals("16") || res.getString("处理状态").equals("18"))) {
					obj.put("reimbursementStatus", "0");
				} else if (res.getString("状态") != null && res.getString("状态").equals("3")) {
					obj.put("reimbursementStatus", "2");
				} else if (res.getString("状态") != null && res.getString("状态").equals("0")) {
					obj.put("reimbursementStatus", "1");
				} else {
					obj.put("reimbursementStatus", "0");
				}

				obj.put("flowCode", this.nullToObject(res.getString("单据编号")));
				obj.put("id", res.getString("id"));
				dataList.add(obj);
			}
			log.info("查询formmain_0728表信息{}" +  dataList);
			for(i = 0; i < dataList.size(); ++i) {
				res = stmt.executeQuery("select field0176 as invoiceId,'0730' from formson_0730 where formmain_id = " + dataList.getJSONObject(i).getString("id"));
				invoiceList = new JSONArray();

				while(res.next()) {
					if (res.getString("invoiceId") != null) {
						obj = new JSONObject();
						obj.put("invoiceId", res.getString(1));
						invoiceList.add(obj);
					}
				}

				dataList.getJSONObject(i).put("invoiceList", invoiceList);
				url = this.ip + "/billfold/inner/v1/invoice/operateReimbursementStatus";
				param = new JSONObject();
				param.put("version", "1.0");
				param.put("nonce", (new Date()).getTime());
				param.put("timestamp", sf.format(new Date()));
				param.put("data", dataList.getJSONObject(i).toJSONString().replaceAll("\"", "\\\""));
				httpclient = new HttpClient();
				method = new PostMethod(url);
				method.getParams().setParameter("http.protocol.content-charset", "utf-8");
				method.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
				se = new StringRequestEntity(param.toJSONString(), "application/json", "UTF-8");
				method.setRequestEntity(se);
				httpclient.executeMethod(method);
				obj = JSONObject.parseObject(method.getResponseBodyAsString());
				log.info("formmain_0728单据编号:" + dataList.getJSONObject(i).getString("flowCode") + ",状态:" + dataList.getJSONObject(i).getString("reimbursementStatus") + ",结果:" + obj);
			}
		} catch (Exception var16) {
			var16.printStackTrace();
		}

	}

	public String nullToObject(Object obj) {
		return obj == null ? "" : "" + obj;
	}
}
